﻿<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.stms.action.Stms9004ActionBean"%>
<%@page import="com.feib.stms.pagination.PaginatedList"%>
<%
// 欄位排序處理
Stms9004ActionBean actionBean = (Stms9004ActionBean) request.getAttribute("actionBean");
if (null != actionBean && null != actionBean.getQueryResult())
{
	PaginatedList queryResult = actionBean.getQueryResult();
	int sortDirection = queryResult.getSortDirection();  //2 : ascending, else: descending
	String sortSpan = "<span class=\"ui-icon ui-icon-circle-triangle-n\"></span>"; //由小到大
	if (1 == sortDirection)
	{
		sortSpan = "<span class=\"ui-icon ui-icon-circle-triangle-s\"></span>"; //由大到小
	}
	
	String sortCriterion = queryResult.getSortCriterion();
	if (null != sortCriterion && 0 != sortCriterion.length())
	{
		if ("parentMenuItem".equalsIgnoreCase(sortCriterion))
		{
			pageContext.setAttribute("parentMenuItemSortedClass", 1==sortDirection?"sortDesc":"sortAsc");
			pageContext.setAttribute("parentMenuItemSortedSpan", sortSpan);
		}
		else if ("menuNo".equalsIgnoreCase(sortCriterion))
		{
			pageContext.setAttribute("menuNoSortedClass", 1==sortDirection?"sortDesc":"sortAsc");
			pageContext.setAttribute("menuNoSortedSpan", sortSpan);
		}
		else if ("actionName".equalsIgnoreCase(sortCriterion))
		{
			pageContext.setAttribute("actionNameSortedClass", 1==sortDirection?"sortDesc":"sortAsc");
			pageContext.setAttribute("actionNameSortedSpan", sortSpan);
		}
	}
	
	// 上下頁鈕處理
	int lastPageNumber = queryResult.getTotalPageCount();
	int currentPageNumber = queryResult.getPageNumber();
	int prePageNumber = currentPageNumber - 1;
	int nextPageNumber = currentPageNumber + 1;
	
	pageContext.setAttribute("lastPageNumber", new Integer(lastPageNumber));
	pageContext.setAttribute("currentPageNumber", new Integer(currentPageNumber));
	pageContext.setAttribute("prePageNumber", new Integer(prePageNumber));
	pageContext.setAttribute("nextPageNumber", new Integer(nextPageNumber));
} // end of if (null != actionBean && null != actionBean.getQueryResult())

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
	<!--	
	$(document).ready(function() {

		$('#doQueryBtn').click(function() {
    		var errorMessage = '';
            if (errorMessage != '')
            {
                showErrorMessageDialog(errorMessage);
                return false;
            }
            
    		return true;
				
		});
		
	<c:if test="${null != actionBean.queryResult && not empty actionBean.queryResult.paginatedList}">
		
		$("[id^='viewDetail-']").click(function() {
			var menuItemPk = this.id.substring(11);
			$('#hidden\\.qryMenuItemPk').val(menuItemPk);
			$("#hidden\\.doUpdateBtn").trigger('click');
		});
		
		$("[id^='a-sortHeader-']").click(function() {
			var sortField = this.id.substring(13);
			$('#hidden\\.queryResult\\.sortCriterion').val(sortField);
			
			var clazz = $(this).attr('class');
			if (null == clazz || 0 == clazz.length)
				$('#hidden\\.queryResult\\.sortDirection').val('1');
			else if ('sortAsc' == clazz)
				$('#hidden\\.queryResult\\.sortDirection').val('1');
			else
				$('#hidden\\.queryResult\\.sortDirection').val('2');
				
			$('#hidden\\.queryResult\\.pageNumber').val('1');
				
			$("#hidden\\.doQueryBtn").trigger('click');
			
			return false;
		});
		
		$("[id^='firstPageBtn']").click(function() {
			$('#hidden\\.queryResult\\.pageNumber').val('1');
			$("#hidden\\.doQueryBtn").trigger('click');
		});
		
		$("[id^='prePageBtn']").click(function() {
			$('#hidden\\.queryResult\\.pageNumber').val('<c:out value="${prePageNumber}"/>');
			$("#hidden\\.doQueryBtn").trigger('click');
		});
		
		$("[id^='nextPageBtn']").click(function() {
			$('#hidden\\.queryResult\\.pageNumber').val('<c:out value="${nextPageNumber}"/>');
			$("#hidden\\.doQueryBtn").trigger('click');
		});
		
		$("[id^='lastPageBtn']").click(function() {
			$('#hidden\\.queryResult\\.pageNumber').val('<c:out value="${lastPageNumber}"/>');
			$("#hidden\\.doQueryBtn").trigger('click');
		});
		
		$("[id^='currentPageText']").change(function() {
			var value = this.value;
			if (value != ''){
				value = value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
				var intRegex = /^\d+$/;
				if(!intRegex.test(value)) {
					showErrorMessageDialog('頁數必須是整數');
					return ;
				}
				
				value = parseInt(value, 10);
				
				if (value < 1){
					showErrorMessageDialog('頁數必須是大於1');
					return ;
				}
				else if (value > <c:out value='${lastPageNumber}'/>){
					showErrorMessageDialog('頁數必須是小於總頁數 (<c:out value="${lastPageNumber}"/>)');
					return ;
				}
				else if (value != <c:out value='${currentPageNumber}'/>){
					$('#hidden\\.queryResult\\.pageNumber').val(value);
					$("#hidden\\.doQueryBtn").trigger('click');
					return false;
				}
				
			}
			else{
				showErrorMessageDialog('頁數不可為空白)');
				return ;
			}
		});
	</c:if>
	<c:if test="${not empty actionBean.message}">
   		showMessageDialog('<c:out value="${actionBean.message }" escapeXml="false"/>');
	</c:if>	
		
	});
	//打印
	function doPrintEvent()
    {
    	divPrint('listForm');
    	return false;
    }

	//-->

	</script>
</head>

<body>   
      	<div id="smallQueryForm"></div> 
	<stripes:form id="queryMenuItemForm" beanclass="com.feib.stms.action.Stms9004ActionBean" method="post" onsubmit="javascript: waitingDialog({});">            
	  	<div id="queryForm">						
			<table>
				<tr>
					<td colspan="5" class="queryHeader">請輸入查詢條件</td>
				</tr>
				<tr>
                    <th>程式類別</th>
                    <td colspan="4">
                        <stripes:select id="qryMenuTypeIdForm" name="qryMenuTypeIdForm">
	                        <stripes:option value="0" label="程式群組"/>
	                        <stripes:option value="1" label="程式功能"/>
                        </stripes:select>
                    </td>
                </tr>
                <tr>
                    <th>程式代號</th>
                    <td colspan="4">
                        <stripes:text id="qryMenuIdForm" maxlength="30" name="qryMenuIdForm" size="15" />
                    </td>
                </tr>
                <tr>
                    <th>程式名稱</th>
                    <td colspan="4">
                    	<stripes:text id="qryActionNameForm" maxlength="50" name="qryActionNameForm" size="15" />
                    </td>
                </tr>                                
                <tr>
                    <td colspan="5" class="queryFunction">
                    <stripes:submit id="doQueryBtn" name="doQuery" value="查詢" class="formBtn"/>
                    <stripes:submit id="doCreateBtn" name="doCreate" value="新增" class="formBtn"/>
				</td>
                </tr>
           </table>
		</div>
	</stripes:form>            
	<!-- 查詢結果  -->
    <div id="listForm">
	<c:if test="${null != actionBean.queryResult && not empty actionBean.queryResult.paginatedList}">
		<div id="paginationBar">
			<input type="button" id="firstPageBtn" class="formBtn" value="第一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
			<input type="button" id="prePageBtn" class="formBtn" value="上一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
			第 <input type="text" id="currentPageText" class="formBtn" value="${currentPageNumber }" size="3" maxlength="4"> / ${lastPageNumber} 頁
			<input type="button" id="nextPageBtn" class="formBtn" value="下一頁" <c:out value='${lastPageNumber<nextPageNumber ? "disabled": "" }'/> />
			<input type="button" id="lastPageBtn" class="formBtn" value="最末頁" <c:out value='${lastPageNumber<=currentPageNumber ? "disabled": "" }'/> />
		</div>
		
		<table id="queryResultTable">
            <thead>
                <tr>
                    <th>序號</th>
                    <th><a id='a-sortHeader-parentMenuItem' href='javascript:void(0);' class="<c:out value='${parentMenuItemSortedClass }'/>"> <c:out value='${parentMenuItemSortedSpan }' escapeXml="false"/>程式類別</a></th>
                    <th><a id='a-sortHeader-menuNo' href='javascript:void(0);' class="<c:out value='${menuNoSortedClass }'/>"> <c:out value='${menuNoSortedSpan }' escapeXml="false"/>程式代碼</a></th>
                    <th><a id='a-sortHeader-actionName' href='javascript:void(0);' class="<c:out value='${actionNameSortedClass }'/>"> <c:out value='${actionNameSortedSpan }' escapeXml="false"/>程式名稱</a></th>
                    <th>修改</th>
                </tr>
            </thead>
            <tbody>
            <c:forEach items="${actionBean.queryResult.paginatedList}" var="viewObj" varStatus="rowstat">    
            <tr>
                <td align="right"><c:out value='${ rowstat.index + 1}'/></td>
                <td>
                	<c:choose>
                	<c:when test="${empty viewObj.parentMenuItem }"><c:out value="程式群組" /></c:when>
                	<c:when test="${!empty viewObj.parentMenuItem }"><c:out value="${viewObj.parentMenuItem.actionName }" /></c:when>
                	</c:choose>
                </td>
                <td><c:out value="${viewObj.menuNo }" /></td>
                <td><c:out value="${viewObj.actionName }" /></td>
                <td>
                	<div id="viewDetail-<c:out value='${viewObj.menuItemPk}'/>" class="widgetsIcon ui-state-default ui-corner-all" title="檢視"><span class="ui-icon ui-icon-circle-triangle-e"></span></div>
                </td>               
            </tr>
			</c:forEach>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="5" class="listFunction"><input type="button" value="列印" onclick="javascript:doPrintEvent();" class="formBtn"/></td>
                </tr>
            </tfoot>
  		</table>
	</c:if>
	</div>
	
	<div style="display: none;">
<stripes:form id="hidden.queryMenuItemForm" beanclass="com.feib.stms.action.Stms9004ActionBean" method="post" onsubmit="javascript: waitingDialog({});">
	<stripes:hidden id="hidden.qryMenuTypeIdForm" name="qryMenuTypeIdForm" />
	<stripes:hidden id="hidden.qryMenuIdForm" name="qryMenuIdForm" />
	<stripes:hidden id="hidden.qryActionNameForm" name="qryActionNameForm" />
	<stripes:hidden id="hidden.queryResult.pageNumber" name="queryResult.pageNumber" />
	<stripes:hidden id="hidden.queryResult.sortCriterion" name="queryResult.sortCriterion" />
	<stripes:hidden id="hidden.queryResult.sortDirection" name="queryResult.sortDirection" />
	<stripes:hidden id="hidden.qryMenuItemPk" name="qryMenuItemPk" />
	<stripes:submit id="hidden.doQueryBtn" name="doQuery" value="查詢" class="formBtn"/>
	<stripes:submit id="hidden.doUpdateBtn" name="doUpdate" value="修改" class="formBtn"/>
	<stripes:submit id="hidden.doExportExcelBtn" name="doExportExcel" value="匯出Excel" class="formBtn"/>
	<stripes:errors></stripes:errors>
</stripes:form>
	</div>
</body>
</html>
